package com.itisyue.fun.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class RequestUtil {
    public static volatile AtomicInteger atomicIntegerOcr = new AtomicInteger(0);
    public static volatile AtomicInteger atomicIntegerPdf = new AtomicInteger(0);
    public static volatile AtomicInteger atomicIntegerReq = new AtomicInteger(0);
    public static Boolean requestPassLimit(Integer limitSize, Integer waitTime){
        long startTime = System.currentTimeMillis();
        while ((System.currentTimeMillis()-startTime)<=waitTime){
            if(atomicIntegerReq.get()>=limitSize){
                try{
                    Thread.sleep(10);
                }catch (Exception e){
                }
                continue;
            }else{
                atomicIntegerReq.getAndIncrement();
                return true;
            }
        }
        return false;
    }
    public static void requestLimitRelease(){
        atomicIntegerReq.getAndDecrement();
    }


    public static Boolean ocrRequestPassLimit(Integer limitSize, Integer waitTime){
        long startTime = System.currentTimeMillis();
        while ((System.currentTimeMillis()-startTime)<=waitTime){
            if(atomicIntegerOcr.get()>=limitSize){
                try{
                    Thread.sleep(10);
                }catch (Exception e){
                }
                continue;
            }else{
                atomicIntegerOcr.getAndIncrement();
                return true;
            }
        }
        return false;
    }
    public static void ocrRequestLimitRelease(){
        atomicIntegerOcr.getAndDecrement();
    }

    public static Boolean pdfRequestPassLimit(Integer limitSize, Integer waitTime){
        long startTime = System.currentTimeMillis();
        while ((System.currentTimeMillis()-startTime)<=waitTime){
            if(atomicIntegerPdf.get()>=limitSize){
                try{
                    Thread.sleep(10);
                }catch (Exception e){
                }
                continue;
            }else{
                atomicIntegerPdf.getAndIncrement();
                return true;
            }
        }
        return false;
    }
    public static void pdfRequestLimitRelease(){
        atomicIntegerPdf.getAndDecrement();
    }
}
